package user;

import book.Library;
import constant.Constant;
import start.LibrarySystem;
import util.RolemissMathException;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 32309
 * Date: 2025-07-17
 * Time: 20:01
 */
public class ProxyUser {
    private User proxyUser;
    Library library = Library.getLibrary();
    public ProxyUser(User user) {
        proxyUser = user;
    }
    public User getRealUser() {
        return proxyUser;
    }
    private void checkRealUserWhetherAdminUser(String s) {
        if (!(proxyUser instanceof Root)) {
            throw new RolemissMathException(s);
        }
    }

    public void handleOperation(int choice) {
        if (proxyUser instanceof Root) {
            // 管理员操作
            switch (choice) {
                case Constant.ZERO:
                    LibrarySystem.start();
                    break;
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    System.out.println("⽆效的操作。");
            }
        }else if (proxyUser instanceof NormalUser) {
            // 普通⽤⼾操作
            switch (choice) {
                case Constant.ZERO:
                    LibrarySystem.start();
                    break;
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
                default:
                    System.out.println("⽆效的操作。");
            }
        }
    }

                    //调⽤菜单
    public int display() {
        if (proxyUser instanceof Root) {
            return ((Root)proxyUser).display();
        }else if (proxyUser instanceof NormalUser){
            return ((NormalUser)proxyUser).display();
        }
        return -1;
    }
    //添加书籍操作
    public void addBook() {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).addBook();
    }
    //更新书籍操作
    public void updateBook() {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).updateBook();
    }
    //移除图书
    public void removeBook() {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).removeBook();
    }
    //查看图书的借阅次数
    public void borrowCount( ) {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).borrowCount();
    }
    //查看最受欢迎的前K本书
    public void generateBook() {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).generateBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).checkInventoryStatus();
    }

    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        checkRealUserWhetherAdminUser("你不是root用户！！！");
        ((Root)proxyUser).checkAndRemoveOldBooks();
    }

    //--------------------------------普通相关⽅法-----------------------------
    private void checkRealUserWhetherNormalUser(String sss) {
        if(!(proxyUser instanceof NormalUser)){
            throw new RolemissMathException(sss);
        }
    }
    //借阅图书
    public void borrowBook() {
        checkRealUserWhetherNormalUser("请切换普通用户");
        ((NormalUser)proxyUser).borrowBook();
    }
    //归还图书
    public void returnBook() {
        checkRealUserWhetherNormalUser("请切换普通用户");
        ((NormalUser)proxyUser).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        checkRealUserWhetherNormalUser("请切换普通用户");
        ((NormalUser)proxyUser).viewBorrowBooks();
    }

}
